<template>
  <div class="shopcar">
    <div class="listbox">
      <div class="list" v-for="(item,index) in shopcar">
        <div class="list-head">
          <div class="top">
            <label class="checkBox">
              <input type="checkbox" :checked="item.checked==true" @click="item.checked = !item.checked" @change="check()">
              <i class="icon iconfont icon-zhengque"></i>
            </label>
            <img class="img" :src="comimgsrc+item.preview" alt="">
            <div class="right">
              <div class="name">
                {{item.name}}
              </div>
              <div class="guige">
                规格：{{item.unit}}
              </div>
            </div>
          </div>
          <div class="down">
            <p class="price">￥{{item.price}} <del>￥{{item.max_price}}</del></p>
            <div class="numbox">
              <div class="btn" @click="subnum(item.id,item.quantity,index)">
                <i class="icon iconfont icon-jian"></i>
              </div>
              <input disabled="disabled" v-model="item.quantity" type="text">
              <div class="btn" @click="addnum(item.id,item.quantity,index)">
                <i class="icon iconfont icon-jiahao"></i>
              </div>
            </div>
          </div>
        </div>
        <div class="list-down">
          <mt-button type="danger" class="del" size="small" @click="delet(item.id,index)">删除</mt-button>
          <p>小计：<span>￥{{item.total_price | numFilter}}</span></p>
        </div>
      </div>
    </div>
    <div class="fixeddown-sc">
      
    </div>
    <div class="fixeddown">
      <div class="btn" @click="js_tjdd()">结算</div>
      <p class="zjg">总价：<span>￥{{total | numFilter}}</span></p>
      <label class="checkBox">
        <input type="checkbox" v-model="allchecked" v-on:change="allcheck()">
        <i class="icon iconfont icon-zhengque"></i>
      </label>
      <p class="qx">全选</p>
    </div>
    <comFooter v-bind:footerindex="3"></comFooter>
  </div>
</template>

<script>
import qs from "qs"
import comFooter from '@/components/common/comfooter'
import store from '@/vuex/store'
import cp1 from '@/assets/image/cp1.jpg';
export default {
  name: 'App',
  data(){
    return{
      cp1,
      total:0,
      allchecked: false,
      shopcar:[],
      comimgsrc:'http://api.mountainfreshes.com',
      checkedArry:[]
    }
  },
  methods:{
    js_tjdd:function(){
      for(let i=0;i<this.shopcar.length;i++ ){
            if(this.shopcar[i].checked==true){
              this.checkedArry.push(this.shopcar[i].id)
            }
          }
          console.log(this.checkedArry)
          if(this.checkedArry.length==0){
            alert('你还没有选择商品，不能结算')
          }else{
            this.$http.post("/index.php/user/cart/settlement",qs.stringify({
              // mobile:this.tel,
              // password:this.pwd
              user_id: this.$store.state.userid,
              ids: this.checkedArry
          }))
          .then(res => {
            console.log(res)
            this.checkedArry=[]
            this.$router.push({ path: '/buystep',query:{cplist: res.data} })
          })
          .catch(error => {
            alert("网络错误")
          });
          }
      
          

    },
    jszj:function(){
      let zj = 0;
      for(let i=0;i<this.shopcar.length;i++){
        if(this.shopcar[i].checked == true){
          zj = zj*1 + this.shopcar[i].total_price
        }
      }
      this.total = zj;
    },
    allcheck:function(){
      if(this.allchecked){
        for(let i=0;i<this.shopcar.length;i++){
          this.shopcar[i].checked = true
        }
      }else{
        for(let i=0;i<this.shopcar.length;i++){
          this.shopcar[i].checked = false
        }
      }
      this.jszj();
    },
    check:function(){
      let num = 0;
      for(let i=0;i<this.shopcar.length;i++){
        if(this.shopcar[i].checked == true){
          num++;
        }
      }
      if(num == this.shopcar.length){
        this.allchecked = true
      }else{
        this.allchecked = false
      }
      this.jszj();
    },
    getshopcar:function(){
        if(this.$store.state.dlzt==1){
              this.$http.post("/index.php/user/cart/index",qs.stringify({
              // mobile:this.tel,
              // password:this.pwd
                  user_id: this.$store.state.userid

              }))
              .then(res => {
                this.shopcar = res.data.row
                this.$store.state.shopcartnum = res.data.row.length
                console.log(this.shopcar)
                
              })
              .catch(error => {
                alert("网络错误")
              });
            }else{
              
            }
    },
    subnum:function(i,num,index){
      if(this.shopcar[index].quantity==1){
        this.shopcar[index].quantity=1;
        this.shopcar[index].total_price = this.shopcar[index].quantity * this.shopcar[index].price
        this.jszj();
      }else{
        // this.shopcar[i].number--;
        // this.shopcar[i].little = this.shopcar[i].number * this.shopcar[i].price
        this.$http.post("/index.php/user/cart/reduce_goods",qs.stringify({
              // mobile:this.tel,
              // password:this.pwd
                  id: i,
                  quantity: num
              }))
              .then(res => {
                console.log(res)
                this.shopcar[index].quantity--;
                this.shopcar[index].total_price = this.shopcar[index].quantity * (this.shopcar[index].price*1)
                this.jszj();
              })
              .catch(error => {
                alert("网络错误")
              });
      }
      
    },
    addnum:function(i,num,index){
      this.$http.post("/index.php/user/cart/increase_goods",qs.stringify({
              // mobile:this.tel,
              // password:this.pwd
                  id: i,
                  quantity: num
              }))
              .then(res => {
                console.log(res)
                this.shopcar[index].quantity++;
                this.shopcar[index].total_price = this.shopcar[index].quantity * (this.shopcar[index].price*1)
                this.jszj();
              })
              .catch(error => {
                alert("网络错误")
              });
      
    },
    delet:function(i,index){
      this.$messagebox.confirm('确定要删除这个商品吗？','提示',{confirmButtonText:'确定',cancelButtonText:'取消'})
      .then(() => {
        this.$http.post("/index.php/user/cart/remove_goods",qs.stringify({
              // mobile:this.tel,
              // password:this.pwd
              user_id: this.$store.state.userid,
              id: i
          }))
          .then(res => {
            this.shopcar.splice(index,1);
            this.check();
          }).catch(error => {
            alert("网络错误")
          });
      }).catch(() => {
                        
      });
    }
  },
  components:{
    comFooter
  },
  filters: {
    numFilter(value) {
      // 截取当前数据到小数点后三位
      let transformVal = Number(value).toFixed(3)
      let realVal = transformVal.substring(0, transformVal.length - 1)
      // num.toFixed(3)获取的是字符串
      return Number(realVal)
    }
  },
  created(){
    this.getshopcar();
  },
  watch:{
    // "shopcar":{
    //       handler:function(old_val,now_val){
    //         console.log(old_val,now_val);
    //     },
    //     deep: true
    //   },
          '$route' (to, from) {
                // this.getinfo()
               //   const toDepth = to.path.split('/').length
                // const fromDepth = from.path.split('/').length
                // this.transitionName = toDepth < fromDepth ? 'slide-right' : 'slide-left'
                // console.log(to.name)
                if(to.name == 'shopcar'){
                  this.getshopcar();
                  this.allchecked=false
                }
              }
        }
}
</script>

<style scoped lang="less">
.fixeddown-sc{
  width: 100%;
  height: .78rem;
}
.fixeddown{
  width: 100%;
  height: .78rem;
  position: fixed;
  left: 0;
  bottom: .98rem;
  background: #fff;
  border-top: .02rem solid #cccccc;
  .btn{
    float: right;
    width: 1.4rem;
    height: 100%;
    line-height: .78rem;
    color: #fff;
    font-size: .24rem;
    background: #24ca72;
  }
  .zjg{
    float: right;
    margin-right: .5rem;
    font-size: .24rem;
    line-height: .78rem;
    span{
      color: #24ca72;
      font-size: .28rem;
    }
  }
  .checkBox{
            float: left;
            width: .32rem;
            height: .32rem;
            display: block;
            position: relative;
            margin-left: .2rem;
            margin-top: .22rem;
            
            input[type='checkbox']{
                width: 100%;
                height: 100%;
                background-color: #fff;
                -webkit-appearance:none;
                outline: none;
                margin: 0;
                padding: 0;
                border: .01rem solid #c9c9c9;
                border-radius: 2px;
            }
            i{
              display: none;
              position: absolute;
              color: #fff;
              left: 0;
              top: 0;
              text-align: center;
              line-height: .32rem;
              width: 100%;
            }
            input[type=checkbox]:checked{
               background: #24ca72;
            }
            input[type=checkbox]:checked + i{
              display: block;
              color: #fff;
              font-size: .24rem;
            }
          }
          .qx{
            float: left;
            line-height: .78rem;
            font-size: .22rem;
            color: #333;
            margin-left: .12rem;
          }
}
.shopcar{
  width: 100%;
  height: auto;
  zoom: 1;
  overflow: hidden;
  background: #eceef3;
  .listbox{
    width: 100%;
    height: auto;
    zoom: 1;
    overflow: hidden;
    .list{
      width: 100%;
      height: auto;
      zoom: 1;
      overflow: hidden;
      background: #fff;
      margin-top: .1rem;
      .list-head{
        padding: .2rem .2rem 0;
        height: 1.9rem;
        border-top: 0.01rem solid #b5b5b5;
        border-bottom: 0.01rem solid #b5b5b5;
        .top{
          height: 1.14rem;
          width: 100%;
          .checkBox{
            float: left;
            width: .32rem;
            height: .32rem;
            display: block;
            position: relative;
            
            
            input[type='checkbox']{
                width: 100%;
                height: 100%;
                background-color: #fff;
                -webkit-appearance:none;
                outline: none;
                margin: 0;
                padding: 0;
                border: .01rem solid #c9c9c9;
                border-radius: 2px;
            }
            i{
              display: none;
              position: absolute;
              color: #fff;
              left: 0;
              top: 0;
              text-align: center;
              line-height: .32rem;
              width: 100%;
            }
            input[type=checkbox]:checked{
               background: #24ca72;
            }
            input[type=checkbox]:checked + i{
              display: block;
              color: #fff;
              font-size: .24rem;
            }
          }
          .img{
            display: block;
            width: 1.56rem;
            height: 100%;
            float: left;
            margin-left: .18rem;
          }
          .right{
            width: 3.8rem;
            height: 100%;
            float: right;
            .name{
              width: 100%;
              height: .68rem;
              line-height: .34rem;
              text-align: left;
              overflow: hidden;
              text-overflow: ellipsis;
              display: -webkit-box;
              -webkit-box-orient: vertical;
              -webkit-line-clamp: 2;
              font-size: .24rem;
              color: #333;
            }
            .guige{
              width: 100%;
              height: .28rem;
              line-height: .28rem;
              overflow: hidden;
              white-space: nowrap;
              text-overflow: ellipsis;
              font-size: .22rem;
              text-align: left;
              margin-top: .18rem;
            }
          }
        }
        .down{
          width: 100%;
          height: .44rem;
          margin-top: .14rem;
          .price{
            font-size: .28rem;
            color: #24ca72;
            line-height: .44rem;
            padding-left: .5rem;
            float: left;
            font-weight: 200;
            del{
              font-weight: normal;
              font-size: .22rem;
              color: #666; 
              padding-left: .1rem;
            }
          }
          .numbox{
            float: right;
            width: 1.72rem;
            height: 100%;
            box-sizing: border-box;
            border: .02rem solid #cccccc;
            .btn{
              width: .4rem;
              height: .4rem;
              float: left;
              i{
                display: block;
                text-align: center;
                line-height: .4rem;
                color: #666;
                font-size: .22rem;
              }
            }
            input{
              display: block;
              height: .4rem;
              width: .88rem;
              border: none;
              box-sizing: border-box;
              text-align: center;
              float: left;
              border-right: .02rem solid #cccccc;
              border-left: .02rem solid #cccccc;
            }
          }
        }
      }
      .list-down{
        height: 48px;
        border-bottom: 0.01rem solid #b5b5b5;
        padding: 0 .2rem;
        .del{
          float: right;
          margin-top: 7.5px;
        }
        p{
          float: right;
          margin-right: .4rem;
          line-height: 48px;
          font-size: 14px;
          span{
            font-size: 16px;
            font-weight: 200;
            color: #24ca72;
          }
        }
      }
    }
  }
}

</style>
